<!doctype html>
<html class="no-js" lang="">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <title>helix-front documentation</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <link rel="icon" type="image/x-icon" href="../images/favicon.ico">
	   <link rel="stylesheet" href="../styles/style.css">
        <link rel="stylesheet" href="../styles/dark.css">
    </head>
    <body>

        <div class="navbar navbar-default navbar-fixed-top visible-xs">
            <a href="../" class="navbar-brand">helix-front documentation</a>
            <button type="button" class="btn btn-default btn-menu ion-ios-menu" id="btn-menu"></button>
        </div>

        <div class="xs-menu menu" id="mobile-menu">
                <div id="book-search-input" role="search"><input type="text" placeholder="Type to search"></div>            <compodoc-menu></compodoc-menu>
        </div>

        <div class="container-fluid main">
           <div class="row main">
               <div class="hidden-xs menu">
                   <compodoc-menu mode="normal"></compodoc-menu>
               </div>
               <!-- START CONTENT -->
               <div class="content class">
                   <div class="content-data">












<ol class="breadcrumb">
  <li>Classes</li>
  <li >UserCtrl</li>
</ol>

<ul class="nav nav-tabs" role="tablist">
        <li class="active">
            <a href="#info" role="tab" id="info-tab" data-toggle="tab" data-link="info">Info</a>
        </li>
        <li >
            <a href="#source" role="tab" id="source-tab" data-toggle="tab" data-link="source">Source</a>
        </li>
</ul>

<div class="tab-content">
    <div class="tab-pane fade active in" id="c-info">
        <p class="comment">
            <h3>File</h3>
        </p>
        <p class="comment">
            <code>server/controllers/user.ts</code>
        </p>






            <section>
    <h3 id="index">Index</h3>
    <table class="table table-sm table-bordered index-table">
        <tbody>

                <tr>
                    <td class="col-md-4">
                        <h6><b>Methods</b></h6>
                    </td>
                </tr>
                <tr>
                    <td class="col-md-4">
                        <ul class="index-list">
                            <li>
                                    <span class="modifier">Protected</span>
                                <a href="#can" >can</a>
                            </li>
                            <li>
                                    <span class="modifier">Protected</span>
                                <a href="#current" >current</a>
                            </li>
                            <li>
                                    <span class="modifier">Protected</span>
                                <a href="#login" >login</a>
                            </li>
                        </ul>
                    </td>
                </tr>





        </tbody>
    </table>
</section>

            <section>
    <h3 id="constructor">Constructor</h3>
        <table class="table table-sm table-bordered">
            <tbody>
                <tr>
                    <td class="col-md-4">
<code>constructor(router: <a href="https://angular.io/api/router/Router" target="_blank">Router</a>)</code>
                    </td>
                </tr>
                        <tr>
                            <td class="col-md-4">
                                <div class="io-line">Defined in <a href="" data-line="15" class="link-to-prism">server/controllers/user.ts:15</a></div>
                            </td>
                        </tr>

                <tr>
                    <td class="col-md-4">
                            <div>
                                    <b>Parameters :</b>
                                    <table class="params">
                                        <thead>
                                            <tr>
                                                <td>Name</td>
                                                    <td>Type</td>
                                                <td>Optional</td>
                                            </tr>
                                        </thead>
                                        <tbody>
                                                <tr>
                                                        <td>router</td>
                                                  
                                                        <td>
                                                                        <code><a href="https://angular.io/api/router/Router" target="_blank" >Router</a></code>
                                                        </td>
                                                  
                                                    <td>
                                                            No
                                                    </td>
                                                    
                                                </tr>
                                        </tbody>
                                    </table>
                            </div>
                    </td>
                </tr>
            </tbody>
        </table>
</section>


            <section>
    
    <h3 id="methods">
        Methods
    </h3>
    <table class="table table-sm table-bordered">
        <tbody>
            <tr>
                <td class="col-md-4">
                    <a name="can"></a>
                    <span class="name">
                        <span class="modifier">Protected</span>
                        <span ><b>can</b></span>
                        <a href="#can"><span class="icon ion-ios-link"></span></a>
                    </span>
                </td>
            </tr>
            <tr>
                <td class="col-md-4">
                    <span class="modifier-icon icon ion-ios-reset"></span>
                    <code>can(req: <a href="../interfaces/HelixRequest.html" target="_self">HelixRequest</a>, res: Response)</code>
                </td>
            </tr>


            <tr>
                <td class="col-md-4">
                    <div class="io-line">Defined in <a href="" data-line="48"
                            class="link-to-prism">server/controllers/user.ts:48</a></div>
                </td>
            </tr>


            <tr>
                <td class="col-md-4">

                    <div class="io-description">
                        <b>Parameters :</b>
                        
                        <table class="params">
                            <thead>
                                <tr>
                                    <td>Name</td>
                                    <td>Type</td>
                                    <td>Optional</td>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>req</td>
                                    <td>
                                                <code><a href="../interfaces/HelixRequest.html" target="_self" >HelixRequest</a></code>
                                    </td>

                                    <td>
                                        No
                                    </td>


                                </tr>
                                <tr>
                                    <td>res</td>
                                    <td>
                                            <code>Response</code>
                                    </td>

                                    <td>
                                        No
                                    </td>


                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <div>
                    </div>
                    <div class="io-description">
                        <b>Returns : </b>        <code><a href="https://www.typescriptlang.org/docs/handbook/basic-types.html" target="_blank" >any</a></code>

                    </div>
                    <div class="io-description">
                        
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
    <table class="table table-sm table-bordered">
        <tbody>
            <tr>
                <td class="col-md-4">
                    <a name="current"></a>
                    <span class="name">
                        <span class="modifier">Protected</span>
                        <span ><b>current</b></span>
                        <a href="#current"><span class="icon ion-ios-link"></span></a>
                    </span>
                </td>
            </tr>
            <tr>
                <td class="col-md-4">
                    <span class="modifier-icon icon ion-ios-reset"></span>
                    <code>current(req: <a href="../interfaces/HelixRequest.html" target="_self">HelixRequest</a>, res: Response)</code>
                </td>
            </tr>


            <tr>
                <td class="col-md-4">
                    <div class="io-line">Defined in <a href="" data-line="39"
                            class="link-to-prism">server/controllers/user.ts:39</a></div>
                </td>
            </tr>


            <tr>
                <td class="col-md-4">

                    <div class="io-description">
                        <b>Parameters :</b>
                        
                        <table class="params">
                            <thead>
                                <tr>
                                    <td>Name</td>
                                    <td>Type</td>
                                    <td>Optional</td>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>req</td>
                                    <td>
                                                <code><a href="../interfaces/HelixRequest.html" target="_self" >HelixRequest</a></code>
                                    </td>

                                    <td>
                                        No
                                    </td>


                                </tr>
                                <tr>
                                    <td>res</td>
                                    <td>
                                            <code>Response</code>
                                    </td>

                                    <td>
                                        No
                                    </td>


                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <div>
                    </div>
                    <div class="io-description">
                        <b>Returns : </b>        <code><a href="https://www.typescriptlang.org/docs/handbook/basic-types.html" target="_blank" >void</a></code>

                    </div>
                    <div class="io-description">
                        
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
    <table class="table table-sm table-bordered">
        <tbody>
            <tr>
                <td class="col-md-4">
                    <a name="login"></a>
                    <span class="name">
                        <span class="modifier">Protected</span>
                        <span ><b>login</b></span>
                        <a href="#login"><span class="icon ion-ios-link"></span></a>
                    </span>
                </td>
            </tr>
            <tr>
                <td class="col-md-4">
                    <span class="modifier-icon icon ion-ios-reset"></span>
                    <code>login(req: <a href="../interfaces/HelixRequest.html" target="_self">HelixRequest</a>, res: Response)</code>
                </td>
            </tr>


            <tr>
                <td class="col-md-4">
                    <div class="io-line">Defined in <a href="" data-line="59"
                            class="link-to-prism">server/controllers/user.ts:59</a></div>
                </td>
            </tr>


            <tr>
                <td class="col-md-4">

                    <div class="io-description">
                        <b>Parameters :</b>
                        
                        <table class="params">
                            <thead>
                                <tr>
                                    <td>Name</td>
                                    <td>Type</td>
                                    <td>Optional</td>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>req</td>
                                    <td>
                                                <code><a href="../interfaces/HelixRequest.html" target="_self" >HelixRequest</a></code>
                                    </td>

                                    <td>
                                        No
                                    </td>


                                </tr>
                                <tr>
                                    <td>res</td>
                                    <td>
                                            <code>Response</code>
                                    </td>

                                    <td>
                                        No
                                    </td>


                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <div>
                    </div>
                    <div class="io-description">
                        <b>Returns : </b>        <code><a href="https://www.typescriptlang.org/docs/handbook/basic-types.html" target="_blank" >void</a></code>

                    </div>
                    <div class="io-description">
                        
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
</section>





    </div>


    <div class="tab-pane fade  tab-source-code" id="c-source">
        <pre class="line-numbers compodoc-sourcecode"><code class="language-typescript">import { Response, Router } from &#x27;express&#x27;;
import * as LdapClient from &#x27;ldapjs&#x27;;
import * as request from &#x27;request&#x27;;
import { readFileSync } from &#x27;fs&#x27;;

import {
  LDAP,
  IDENTITY_TOKEN_SOURCE,
  CUSTOM_IDENTITY_TOKEN_REQUEST_BODY,
  SSL,
} from &#x27;../config&#x27;;
import { HelixRequest, HelixRequestOptions } from &#x27;./d&#x27;;
import { TOKEN_EXPIRATION_KEY, TOKEN_RESPONSE_KEY } from &#x27;../config&#x27;;

export class UserCtrl {
  constructor(router: Router) {
    // uncomment the following line to use customized login
    // router.route(&#x27;/user/authorize&#x27;).get(this.authorize);
    router.route(&#x27;/user/login&#x27;).post(this.login.bind(this));
    router.route(&#x27;/user/current&#x27;).get(this.current);
    router.route(&#x27;/user/can&#x27;).get(this.can);
  }

  //
  // You may rewrite this function to support your own authorization logic.
  // Usually it would be helpful to integrate with 3rd party login.
  // For example:
  //
  /*
  protected authorize(req: HelixRequest, res: Response) {
    const { isAdmin, username, token } &#x3D; get_auth_state();
    req.session.isAdmin &#x3D; isAdmin;
    req.session.username &#x3D; username;
    req.session.identityToken &#x3D; token;
    res.redirect(&#x27;/&#x27;);
  }
  */

  protected current(req: HelixRequest, res: Response) {
    res.json(req.session.username || &#x27;Sign In&#x27;);
  }

  //
  // Check the server-side session store,
  // see if this helix-front ExpressJS server
  // already knows that the current user is an admin.
  //
  protected can(req: HelixRequest, res: Response) {
    try {
      return res.json(req.session.isAdmin ? true : false);
    } catch (err) {
      throw new Error(
        &#x60;Error from /can logged in admin user session status endpoint: ${err}&#x60;
      );
      return false;
    }
  }

  protected login(req: HelixRequest, res: Response) {
    const credential &#x3D; req.body;
    if (!credential.username || !credential.password) {
      res.status(401).json(false);
      return;
    }

    // check LDAP
    const ldap &#x3D; LdapClient.createClient({ url: LDAP.uri });
    ldap.bind(
      credential.username + LDAP.principalSuffix,
      credential.password,
      (err) &#x3D;&gt; {
        if (err) {
          res.status(401).json(false);
        } else {
          // LDAP login success
          const opts &#x3D; {
            filter:
              &#x27;(&amp;(sAMAccountName&#x3D;&#x27; +
              credential.username +
              &#x27;)(objectcategory&#x3D;person))&#x27;,
            scope: &#x27;sub&#x27;,
          };

          req.session.username &#x3D; credential.username;
          res.set(&#x27;Username&#x27;, credential.username);

          ldap.search(LDAP.base, opts, function (err, result) {
            let isInAdminGroup &#x3D; false;
            result.on(&#x27;searchEntry&#x27;, function (entry) {
              if (entry.object &amp;&amp; !err) {
                const groups &#x3D; entry.object[&#x27;memberOf&#x27;];
                for (const group of groups) {
                  const groupName &#x3D; group.split(&#x27;,&#x27;, 1)[0].split(&#x27;&#x3D;&#x27;)[1];
                  if (groupName &#x3D;&#x3D; LDAP.adminGroup) {
                    isInAdminGroup &#x3D; true;

                    //
                    // Get an Identity-Token
                    // if an IDENTITY_TOKEN_SOURCE
                    // is specified in the config
                    //
                    if (IDENTITY_TOKEN_SOURCE) {
                      const body &#x3D; JSON.stringify({
                        username: credential.username,
                        password: credential.password,
                        ...CUSTOM_IDENTITY_TOKEN_REQUEST_BODY,
                      });

                      const options: HelixRequestOptions &#x3D; {
                        url: IDENTITY_TOKEN_SOURCE,
                        json: &#x27;&#x27;,
                        body,
                        headers: {
                          &#x27;Content-Type&#x27;: &#x27;application/json&#x27;,
                        },
                        agentOptions: {
                          rejectUnauthorized: false,
                        },
                      };

                      if (SSL.cafiles.length &gt; 0) {
                        options.agentOptions.ca &#x3D; readFileSync(SSL.cafiles[0], {
                          encoding: &#x27;utf-8&#x27;,
                        });
                      }

                      function callback(error, _res, body) {
                        if (error) {
                          throw new Error(
                            &#x60;Failed to get ${IDENTITY_TOKEN_SOURCE} Token: ${error}&#x60;
                          );
                        } else if (body?.error) {
                          throw new Error(body?.error);
                        } else {
                          const parsedBody &#x3D; JSON.parse(body);
                          req.session.isAdmin &#x3D; isInAdminGroup;
                          req.session.identityToken &#x3D; parsedBody;

                          const cookieName &#x3D; &#x27;helixui_identity.token&#x27;;
                          const cookieValue &#x3D;
                            parsedBody.value[TOKEN_RESPONSE_KEY];
                          const cookieExpiresDate &#x3D; new Date(
                            parsedBody.value[TOKEN_EXPIRATION_KEY]
                          );
                          const cookieOptions &#x3D; {
                            expires: cookieExpiresDate,
                          };
                          res.cookie(cookieName, cookieValue, cookieOptions);
                          res.json(isInAdminGroup);

                          return parsedBody;
                        }
                      }
                      request.post(options, callback);
                    } else {
                      req.session.isAdmin &#x3D; isInAdminGroup;
                      res.json(isInAdminGroup);
                    }
                    //
                    // END Get an Identity-Token
                    //
                  }
                }
              } else {
                req.session.isAdmin &#x3D; isInAdminGroup;
                res.json(isInAdminGroup);
              }
            });
          });
        }
      }
    );
  }
}
</code></pre>
    </div>
</div>









                   </div><div class="search-results">
    <div class="has-results">
        <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
        <ul class="search-results-list"></ul>
    </div>
    <div class="no-results">
        <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
    </div>
</div>
</div>
               <!-- END CONTENT -->
           </div>
       </div>

          <label class="dark-mode-switch">
               <input type="checkbox">
               <span class="slider">
                    <svg class="slider-icon" viewBox="0 0 24 24" fill="none" height="20" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" width="20" xmlns="http://www.w3.org/2000/svg">
                    <path d="M21 12.79A9 9 0 1111.21 3 7 7 0 0021 12.79z"></path>
                    </svg>
               </span>
          </label>

       <script>
            var COMPODOC_CURRENT_PAGE_DEPTH = 1;
            var COMPODOC_CURRENT_PAGE_CONTEXT = 'class';
            var COMPODOC_CURRENT_PAGE_URL = 'UserCtrl.html';
            var MAX_SEARCH_RESULTS = 15;
       </script>

       <script src="../js/libs/custom-elements.min.js"></script>
       <script src="../js/libs/lit-html.js"></script>

       <script src="../js/menu-wc.js" defer></script>
       <script nomodule src="../js/menu-wc_es5.js" defer></script>

       <script src="../js/libs/bootstrap-native.js"></script>

       <script src="../js/libs/es6-shim.min.js"></script>
       <script src="../js/libs/EventDispatcher.js"></script>
       <script src="../js/libs/promise.min.js"></script>
       <script src="../js/libs/zepto.min.js"></script>

       <script src="../js/compodoc.js"></script>

       <script src="../js/tabs.js"></script>
       <script src="../js/menu.js"></script>
       <script src="../js/libs/clipboard.min.js"></script>
       <script src="../js/libs/prism.js"></script>
       <script src="../js/sourceCode.js"></script>
          <script src="../js/search/search.js"></script>
          <script src="../js/search/lunr.min.js"></script>
          <script src="../js/search/search-lunr.js"></script>
          <script src="../js/search/search_index.js"></script>
       <script src="../js/lazy-load-graphs.js"></script>


    </body>
</html>
